﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BadHabit.Factory.Tail {

    public class PostCommitHook : BaseHook {


        /// <summary>
        /// Main program.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args) {

            // gets the repository and the transaction
            if (args.Length > 1) {
                Repository = args[0];
                int rev = 0;                
                int.TryParse(args[1], out rev);
                Revision = rev;
            } else {
                Console.WriteLine("Use: post-commit Repository Revision");
                return;
            }

            // Get files, user, and comment

            string[] filesChanged;
            try {                
                filesChanged = GetFilesChanged();
            } catch (Exception ex) {
                System.Console.Error.WriteLine(ex);
                Environment.ExitCode = 1;
                return;
            }


            string[] filesToTail = CleanFileList(filesChanged);

            string user = GetMadeByUserByRevision();

            string comment = GetCommentByRevision();

            // if is not tailed task, get out
            if (!IsTailed(comment))
                return;

            // get the task Id
            int taskId = GetTaskId(comment);
            
            // get resolve task number
            bool resolveTaskNumber = false;
            resolveTaskNumber = GetResolveTaskNumber();
            
            // no task Id, return error
            if (taskId < 0 && resolveTaskNumber == false) {
                System.Console.Error.WriteLine("Please, provide a task number in order to commit your job.");
                Environment.ExitCode = 1;
                return;
            }

            // sets the server    
            try {
                SetFactoryServer();
            } catch (Exception) {
                System.Console.Error.WriteLine("Could not find the Scrum Factory Server.");
                Environment.ExitCode = 1;
                return;
            }

            string repositoryUrl;
            
            repositoryUrl = GetRepositoryUrl();


            FactoryService.TaskDataSet dsTask = new BadHabit.Factory.Tail.FactoryService.TaskDataSet();
            
            try {
                // if a task was informed, tail it by the task id
                // otherwise tail it by the current working task of the user
                if (taskId > 0)
                    dsTask = service.TailTaskById(repositoryUrl + "/", taskId, filesToTail, Revision);
                else
                    dsTask = service.TailTaskByTeamMember(repositoryUrl + "/", user, filesToTail, Revision);

            } catch (Exception ex) {
                System.Console.Error.WriteLine("Scrum Factory: Error tailing task " + taskId + ".\n\n" + ex.Message);
                Environment.ExitCode = 1;
                return;
            }

            if(dsTask.Task.Count>0)
                SetComment(dsTask.Task[0].TaskName + "(#" + dsTask.Task[0].TaskId + "#)");

        }

    }
}
